package cn.sciento.boot.api.customize.controller;

import cn.sciento.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import cn.sciento.boot.api.customize.commons.vo.MethodMetaData;
import cn.sciento.boot.api.customize.service.ApiCustomizeService;
import cn.sciento.boot.api.customize.service.ApiScanService;
import cn.sciento.core.util.Results;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomizeController {
  private final ApiScanService apiScanService;
  
  private final ApiCustomizeService apiCustomizeService;
  
  @Value("${spring.application.name}")
  private String applicationName;
  
  public CustomizeController(ApiScanService apiScanService, ApiCustomizeService apiCustomizeService) {
    this.apiScanService = apiScanService;
    this.apiCustomizeService = apiCustomizeService;
  }
  
  @ApiOperation("扫描服务类及方法")
  @Permission(permissionWithin = true)
  @GetMapping({"/v1/customize/scan"})
  public ResponseEntity<List<MethodMetaData>> scan(@RequestParam ArrayList<String> packageNames) {
    return Results.success(this.apiScanService.scanMethod(packageNames));
  }
  
  @ApiOperation("客制化服务")
  @Permission(permissionWithin = true)
  @GetMapping({"/v1/customize/apply"})
  public ResponseEntity<Void> apply(ArrayList<String> ruleCodes) {
    this.apiCustomizeService.customize(this.applicationName, ruleCodes);
    return Results.success();
  }
}
